<?php
/**
 * 图片类型feed类
 *
 * 类名
 * ml_model_dbPicFeed
 * @zhanglei11
 *
 */
class ml_model_dbPicFeed extends Lib_datamodel_db
{

    /**
     * 创建构造函数
     *
     */
    function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');
        parent::__construct('pic_feed' , $db_config['pic_feed']);
    }

    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['feed_info']))
        {
            $data['feed_info'] = unserialize($data['feed_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['feed_info']))
                {
                    $val['feed_info'] =unserialize($val['feed_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['feed_info']))
        {
            $array['feed_info'] = serialize($array['feed_info']);
        }
        return $array;
    }

    /**
     * 增加feed
     * @param array $data
     */
    public function add_feed($data )
    {

        if(!$this->init_db())
        return false;
        $data['from'] = empty($data['from']) ? ML_FROM_WEB : $data['from'];
        $rs = $this->insert($data);

        if(!$rs)
        return false;
        else
        {
            //    ?? 记这个干嘛的
            //            $rid = ml_tool_resid::make_resid($uid , ML_RESID_TYPE_ATME , $this->insert_id());
            return false;
        }
    }


    /**
     * 根据传入的uid 数组返回图片类型的feed列表
     * @param unknown_type $uid
     * @param unknown_type $start
     * @param unknown_type $pagesize
     * @param unknown_type $uids
     */
    public function getListByUids($uid, $start = 0 , $pagesize = 40,$uids = array())
    {
        if(!$this->init_db('',self::DB_SLAVE))
        return false;



        $where = '';
        if(count($uids) > 0 && is_array($uids)) {
                
            $uids = implode(',',$uids);
            $where = ' `uid` in ('.$uids.')  or ';
            $where .= ' (uid = '.$uid.' and isown=1) ';
        }else {
            $where = 'uid = '.$uid.' and isown=1';
        }

        $sql = 'select * from `'.$this->table.'` where '.$where.' order by `ctime` desc limit '.$start.' , '.$pagesize;

        return $this->query($sql,true);
    }


    /**
     * 根据传入的uid 数组返回图片类型的feed的总数
     * @param int $uid
     * @param int $start
     * @param int $pagesize
     * @param array $uids
     */
    public function getCntByUids($uid, $uids = array())
    {
        if(!$this->init_db())
        return false;



        $where = '';
        if(count($uids) > 0 && is_array($uids)) {

            $uids = implode(',',$uids);
            $where = ' `uid` in ('.$uids.')  or ';
        }
        $where .= ' (uid = '.$uid.' and isown=1) ';

        $sql = 'select count(*) as num from `'.$this->table.'` where '.$where.' order by `ctime` desc ';
        return $this->fetch($sql);
    }

    /**
     * 根据pubfeed_id删除feed记录
     * @param int $pubfeed_id
     */
    public function delByUserFeedId($userfeed_rid)
    {
        if(!$this->init_db())    //    这个不分表的是不是可以放到 构造函数里？
        return false;
        $sql = 'delete from `'.$this->table.'` where `userfeed_rid` = \''.$userfeed_rid.'\'';
        return $this->query($sql,true);

    }

    /**
     * 根据rid删feed
     * @param int ￥rids
     * xinhua
     */
    public function delByContent_rid($content_rid)
    {
        if(!$this->init_db())    //    这个不分表的是不是可以放到 构造函数里？
          return false;

        $sql = "delete from `$this->table` where `content_rid` = '$content_rid' ";//磊哥这里用delete？
        return $this->query($sql,true);

    }
}
?>